package com.banshou.installs.ui.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.banshou.installs.R;
import com.banshou.installs.constant.BaseApplication;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.HomeBinding;
import com.banshou.installs.ui.activity.ScreenInfoActivity;
import com.blankj.utilcode.util.ToastUtils;
import com.xys.libzxing.zxing.activity.CaptureActivity;

import static android.app.Activity.RESULT_OK;

/**
 * Created by cjq on 2017/12/29.
 * Email: stoic_yb@139.com
 * features: 首页
 */

public class HomeFragment extends BaseFragment {
    private HomeBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.home, container, false);
        initView();
        return mBinding.getRoot();
    }

    private void initView() {
        mBinding.next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(getActivity(), CaptureActivity.class), 0);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
            String result = data.getExtras().getString(Config.TRANSDATA);
            if (result != null) {
                if (result.contains("BS:")) {
                    Intent intent = new Intent(getActivity(), ScreenInfoActivity.class);
                    intent.putExtra(Config.TRANSDATA, result.replace("BS:", ""));
                    startActivity(intent);
                } else {
                    Toast.makeText(BaseApplication.getContext(), "请选择正确的二维码进行扫描", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
